
import util from "/common/js/util.js";
import Qs from "qs";

var app = getApp();
const pageSize = 6;

Page({
  data: {
    STATUS_VALUE:"",
    scrollTop: 0,
    toView: "",
    showLoadMoreTxt: "over",
    dataList:[],
    page: 1,
    pageSize: pageSize,
    totalResult: pageSize,
    totalnum: 0,
  },
  onLoad() {
    this.searchFn()
  },
  scroll(e) {
    this.setData({
      scrollTop: e.detail.scrollTop,
      toView: ""
    })
  },
  tabClick(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      STATUS_VALUE: status,
      totalnum: 0,
      dataList: [],
      page: 1,
      totalResult: pageSize,
    });
    this.tableList();
  },

  searchFn() {
    this.setData({
      page: 1,
      totalnum: 0,
      dataList: []
    });
    this.tableList()
  },
  parseStatusValue(item){
    let status_value="";
    switch (item.status) {
      case 0:
        status_value='待取货';
        break;
      case 1:
        status_value='已取货';
        break;
      case 2:
        status_value='订单准备中';
        break;      
      case 99:
        status_value='已取消';
        break;    
      default:
        break;
    }
    item.status_value=status_value;
  },
  tableList(obj){
    let api = "/user/good/orderList";
    let list = {
      page: this.data.page,
      pageSize: this.data.pageSize,
      status: this.data.STATUS_VALUE
    };
    
    //console.log("--list:",list);
    let that = this;
    this.setData({
      showLoadMoreTxt: "load"
    });
    
    if (this.data.dataList.length >= this.data.totalResult&&this.data.totalResult!=0) {
      this.setData({
        showLoadMoreTxt: "over"
      })
      dd.stopPullDownRefresh();
      return false;
    }
    dd.showLoading({
        content: '数据加载中...',
      })
    let dataList=this.data.dataList;
    app.$post(api, list).then(res => {
      dd.stopPullDownRefresh()
     dd.hideLoading();
      if (res.code == 200) {
        let arr=res.data.list;
        arr.forEach(row=>{
          this.parseStatusValue(row);
        })
        let arr_list=dataList.concat(arr);
        let page=this.data.page;
        page=page+1;
        
        this.setData({
          totalResult: res.data.totalResult,
          page,
          dataList: arr_list,
          showLoadMoreTxt: "over"
        });
        
      }else{
        util.message(res.message);

      }
    })
  },
  onScrollToLower() {
    // console.log("---触底");
    this.tableList()
  },
  onPullDownRefresh() {
   this.searchFn();
  },

  onReceiveFn(e){
    let index = e.currentTarget.dataset.index;

    dd.confirm({
      title: '温馨提示',
      content: '是否已收到货物?',
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          this.receive(index);
        }
      },
    });
  },
  onCancelFn(e){
    let index = e.currentTarget.dataset.index;

    dd.confirm({
      title: '温馨提示',
      content: '是否取消该订单?',
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      success: (result) => {
        if (result.confirm) {
          this.cancel(index);
        }
      },
    });
  },
  receive(index){
    let dataList=this.data.dataList;
    let item= dataList[index];
    let api = "/user/good/takesOrder", list = {orderId:item.orderId};
    app.$post(api, list).then(re => {
      try {
         if (re.code == 200) {
          this.searchFn()
          
        }else {
          util.message(re.message)
        };
      } catch (error) {
        
      }

    }).catch(err => {
    });
  },
  cancel(index){
    let dataList=this.data.dataList;
    let item= dataList[index];
    let api = "/user/good/orderCancel", list = {orderId:item.orderId};
    app.$post(api, list).then(re => {
      try {
         if (re.code == 200) {
          this.searchFn()
          
        }else {
          util.message(re.message)
        };
      } catch (error) {
        
      }

    }).catch(err => {
    });
  },
  goToShopHome(e){
    let index = e.currentTarget.dataset.index;
    let dataList=this.data.dataList;
    let item= dataList[index];
    // let item = e.currentTarget.dataset.item;
    let json={goodId:item.items[0].goodId}
    let p = Qs.stringify(json);
    let path = '/pages/shop/shop_detail/shop_detail';
    let url = `${path}?${p}`;
    dd.navigateTo({
      url
    })
  }
});
